#include "account.h"

//注册

//type
char* funType(char *buf,char *type)
{
    //定义cJSON指针
    cJSON *json = NULL;
    //将字符串转为json格式对象
    json = cJSON_Parse(buf);

    cJSON *node = NULL;
    //由键获取值
    node = cJSON_GetObjectItem(json,"type");
    if(node == NULL)
    {
        LOG_E("can't find\n");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcpy(type,node->valuestring);
    }
    return type;
}

//管理员注册
void managerReguster(int sock,char *buf)
{
    int ret = 0;
    char type[30] = {0};
    struct regulator info;
    memset(&info,0,sizeof(info));

    //定义cJSON指针
    cJSON *json = NULL;
    //将字符串转为json格式对象
    json = cJSON_Parse(buf);

    cJSON *node = NULL;
    //由键获取值
    node = cJSON_GetObjectItem(json,"type");
    if(node == NULL)
    {
        LOG_E("can't find type\n");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcmp(type,node->valuestring);
    }

    node = cJSON_GetObjectItem(json,"id");
    if(node == NULL)
    {
        LOG_E("can't find id\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        info.id = node->valueint;
    }

    node = cJSON_GetObjectItem(json,"name");
    if(node == NULL)
    {
        LOG_E("can't find name\n");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcpy(info.name,node->valuestring);
    }

    node = cJSON_GetObjectItem(json,"gender");
    if(node == NULL)
    {
        LOG_E("can't find gender\n");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcpy(info.gender,node->valuestring);
    }

    node = cJSON_GetObjectItem(json,"age");
    if(node == NULL)
    {
        LOG_E("can't find age\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        info.age = node->valueint;
    }

    node = cJSON_GetObjectItem(json,"account");
    if(node == NULL)
    {
        LOG_E("can't find account\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        info.account = node->valueint;
    }

    node = cJSON_GetObjectItem(json,"cipher");
    if(node == NULL)
    {
        LOG_E("can't find cipher\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        info.cipher = node->valueint;
    }
    cJSON_Delete(json);	

    mysqlInfo(info,buf);//数据库

    int value = 0;

    if(strcmp(type,"Administratorregistration") == 0)
    {        
        LOG_D("value = %d\n",value);
        value = 1;//成功
    }
    else
    {
        LOG_D("value = %d\n",value);
        value = 0;//失败
    }
    LOG_D("strcmp : %d\n",strcmp(type,"Administratorregistration"));

    ret = send(sock,&value,4,0);
    if(ret == -1)
    {
        //perror("sendto");
        LOG_E("sendto\n"); 
        exit(-1);
    }
    close(sock);
}

//员工注册
void staffReguster(int sock,char *buf)
{
    int ret = 0;
    char type[30] = {0};
    struct staff info;
    memset(&info,0,sizeof(info));

    //定义cJSON指针
    cJSON *json = NULL;
    //将字符串转为json格式对象
    json = cJSON_Parse(buf);

    cJSON *node = NULL;
    //由键获取值
    node = cJSON_GetObjectItem(json,"type");
    if(node == NULL)
    {
        LOG_E("can't find type\n");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcmp(type,node->valuestring);
    }

    node = cJSON_GetObjectItem(json,"uid");
    if(node == NULL)
    {
        LOG_E("can't find uid\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        info.uid = node->valueint;
    }

    node = cJSON_GetObjectItem(json,"name");
    if(node == NULL)
    {
        LOG_E("can't find name\n");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcpy(info.name,node->valuestring);
    }

    node = cJSON_GetObjectItem(json,"gender");
    if(node == NULL)
    {
        LOG_E("can't find gender\n");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcpy(info.gender,node->valuestring);
    }

    node = cJSON_GetObjectItem(json,"age");
    if(node == NULL)
    {
        LOG_E("can't find age\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        info.age = node->valueint;
    }

    node = cJSON_GetObjectItem(json,"account");
    if(node == NULL)
    {
        LOG_E("can't find account\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        info.account = node->valueint;
    }

    node = cJSON_GetObjectItem(json,"cipher");
    if(node == NULL)
    {
        LOG_E("can't find cipher\n");
        exit(-1);
    }
    if(node->type == cJSON_Number)
    {
        info.cipher = node->valueint;
    }
    cJSON_Delete(json);	

   mysqlInfoStaff(info,buf);//数据库

    int value = 0;

    if(strcmp(type,"Employeeregistration") == 0)
    {        
        LOG_D("value = %d\n",value);
        value = 1;//成功
    }
   
    else
    {
        LOG_D("value = %d\n",value);
        value = 0;//失败
    }
    LOG_D("strcmp : %d\n",strcmp(type,"Employeeregistration"));

    ret = send(sock,&value,4,0);
    if(ret == -1)
    {
        //perror("sendto");
        LOG_E("sendto\n"); 
        exit(-1);
    }
    close(sock);
}
